<template></template>

<script lang="ts" setup>
import { useRouter } from 'vue-router'
import { useLoginUserStore } from '@/stores/useLoginUserStore.ts'
import { listSpaceByPageUsingPost } from '@/api/spaceController.ts'
import { onMounted } from 'vue'

const router = useRouter()
const loginUserStore = useLoginUserStore()

const loginUser = loginUserStore.loginUser

const checkUserSpace = async () => {
  //用户未登录，跳转到登录页
  if (!loginUser?.id) {
    await router.replace('/user/login')
  } else {
    const res = await listSpaceByPageUsingPost({
      userId: loginUser.id,
      current: 1,
      pageSize: 1,
    })
    if (res.data.code == 0) {
      if (res.data.data && res.data.data.records.length > 0) {
        //如果有就跳转到第一个空间
        const space = res.data.data.records[0]
        await router.replace(`/space/${space.id}`)
      } else {
        //如果没有个人空间就跳转到创建个人空间页
        await router.replace('/add_space')
      }
    }
  }
}

onMounted(() => {
  checkUserSpace()
})
</script>
